package com.travel.dao.impl;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.travel.dao.HotelOrderDao;
import com.travel.entity.HotelOrder;
import com.travel.entity.OrderStatus;
import com.travel.entity.Room;
import com.travel.entity.ScenicComment;
import com.travel.entity.User;
import com.travel.util.DBUtils;
import com.travel.util.PageInfo;

/**
* @Title HotelOrderDaoImpl.java
* @Package com.travel.dao.impl
* @Author zengbochao
* @Time 2020年10月30日 上午8:57:43
* @Version 1.0
* @Description: TODO
*/
public class HotelOrderDaoImpl implements HotelOrderDao {
	
	private DBUtils db=new DBUtils();
	
	@Override
	public PageInfo<HotelOrder> findByPage(int pageNum, int pageSize, String userName) {
		List<HotelOrder> data=new ArrayList<HotelOrder>();
		
		StringBuffer sb=new StringBuffer();
		sb.append("select")
		.append(" O.order_id,")
		.append(" O.user_id,")
		.append(" O.room_id,")
		.append(" O.order_price,")
		.append(" O.order_datetime,")
		.append(" O.status_id,")
		.append(" U.user_name,")
		.append(" R.room_name,")
		.append(" S.status_name")
		.append(" FROM tab_hotel_order O")
		.append(" left JOIN tab_user U")
		.append(" ON O.user_id=U.user_id")
		.append(" left JOIN tab_room R")
		.append(" ON O.room_id=R.room_id")
		.append(" left JOIN tab_order_status S")
		.append(" ON S.status_id=O.status_id")
		.append(" where U.user_name like concat('%',?,'%')")
		.append(" LIMIT ?,?");
		ResultSet rs=db.query(sb.toString(), userName,(pageNum-1)*pageSize,pageSize);
		try {
			while (rs.next()) {
				Integer orderId=rs.getInt("order_id");
				Integer userId=rs.getInt("user_id");
				Integer roomId=rs.getInt("room_id");
				BigDecimal orderPrice=rs.getBigDecimal("order_price");
				String orderDatetime=rs.getString("order_datetime");
				Integer statusId=rs.getInt("status_id");
				String userName2=rs.getString("user_name");
				String roomName=rs.getString("room_name");
				String statusName=rs.getString("status_name");
				
				HotelOrder hotelOrder=new HotelOrder(orderId,userId,roomId,orderPrice,orderDatetime,statusId,new User(null, userName2, null, null, null, null, null, null),new Room(null, null, roomName, null, null, null),new OrderStatus(null, statusName));
				
				data.add(hotelOrder);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int total=0;
		sb=new StringBuffer();
		sb.append("select")
		.append(" count(*)")
		.append(" FROM tab_hotel_order O")
		.append(" INNER JOIN tab_user U")
		.append(" ON O.user_id=U.user_id")
		.append(" INNER JOIN tab_room R")
		.append(" ON O.user_id=R.room_id")
		.append(" INNER JOIN tab_order_status S")
		.append(" ON S.status_id=O.status_id")
		.append(" where U.user_name like concat('%',?,'%')");
		
		rs=db.query(sb.toString(), userName);
		try {
			while (rs.next()) {
				total  = rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// 构建分页对象
		PageInfo<HotelOrder> pageInfo = new PageInfo<HotelOrder>(data, pageNum, pageSize, total);
		
		return pageInfo;
	}

	@Override
	public int insert(HotelOrder hotelOrder) {
		// TODO Auto-generated method stub
		return db.update("INSERT INTO tab_hotel_order(order_id,user_id,room_id,order_price,order_datetime,status_id) VALUES(?,?,?,?,?,?)",hotelOrder.getHotelOrderId(),hotelOrder.getUserId(),hotelOrder.getRoomId(),hotelOrder.getHotelOrderPice(),hotelOrder.getHotelOrderDatetime(),hotelOrder.getHotelStatusId());
	}

	@Override
	public int deleteById(int hotelOrderId) {
		// TODO Auto-generated method stub
		return db.update("delete from tab_hotel_order where order_id=?", hotelOrderId);
	}

	@Override
	public int update(HotelOrder hotelOrder) {
		// TODO Auto-generated method stub
		return 0;
	}

}
